package com.tzxs.pay.main.print.viewmodel;

import android.app.Application;
import android.arch.lifecycle.AndroidViewModel;
import android.databinding.ObservableArrayList;
import android.databinding.ObservableField;
import android.support.annotation.NonNull;
import android.util.Log;

import com.tzxs.pay.main.print.datasource.PrintDataSource;
import com.tzxs.pay.main.print.model.AllPrintBean;
import com.tzxs.pay.main.print.model.UnsetPrintBean;
import com.tzxs.pay.util.SingleLiveEvent;
import com.tzxs.pay.util.netnavigator.SimpleNetNavigator;
import com.tzxs.pay.util.viewmodel.Callback;

public class PrintViewModel extends AndroidViewModel {


    public SingleLiveEvent<String> snackEvent = new SingleLiveEvent<>();
    public SingleLiveEvent<String> dialogEvent = new SingleLiveEvent<>();

    //上面的汇总打印
    public ObservableArrayList<String> printAll = new ObservableArrayList<>();
    //下面的未结算账单打印
    public ObservableArrayList<String> printList = new ObservableArrayList<>();

    public PrintViewModel(@NonNull Application application) {
        super(application);
    }


    //获取打印汇总内容
    public void printAll(int timeType, String timeStart, String timeEnd, Callback callback) {
        PrintDataSource.getInstance(getApplication()).printAll(timeType, timeStart, timeEnd,
                new SimpleNetNavigator<AllPrintBean>(snackEvent, dialogEvent, callback) {
                    @Override
                    public String getTitle() {
                        return "获取打印信息...";
                    }

                    @Override
                    public void success(AllPrintBean data) {
                        //获取打印内容成功
                        if (data.getCode() == 10001) {
                            printAll.clear();
                            printAll.addAll(data.getData().get(0).getContent());
                            callback.success();
                        }
                        snackEvent.setValue(data.getMsg());
                    }

                    @Override
                    public void fail(String error) {
                        snackEvent.setValue(error);
                    }
                });


    }

    //未结算打印
    public void unsetPrint(int timeType, String timeStart, String timeEnd,
                           String print1, String print2, Callback callback) {

        Log.d("23233223", "unsetPrint: 此时上传的参数:  timeStart  " + timeStart + "timeEnd:  " + timeEnd + "print1:  " + print1 + "print2  " + print2 + "timeType:  " + timeType);
        PrintDataSource.getInstance(getApplication()).unsetPrint(timeType, timeStart, timeEnd,
                print1, print2, new SimpleNetNavigator<UnsetPrintBean>(snackEvent, dialogEvent, callback) {
                    @Override
                    public String getTitle() {
                        return "正在获取打印数据...";
                    }

                    @Override
                    public void success(UnsetPrintBean data) {

                        if (data.getCode() == 10001) {
                            printList.clear();
                            printList.addAll(data.getData().get(0).getContent());
                            Log.d("23233223", "success: 内容大小:" + printList.size());
                            for (String st :
                                    printList) {
                                Log.d("23233223", "success: 打印内容:" + st);
                            }
                            callback.success();
                        }
                    }

                    @Override
                    public void fail(String error) {
                        snackEvent.setValue(error);
                    }
                });
    }

}
